package liruilong;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class DoubleColorBall {
    public static int[] generateLotteryNumber() {
        Random random = new Random();
        int[] lotteryNumber = new int[7];
        Set<Integer> set = new HashSet<>();
        while (set.size() < 6) {
            int num = random.nextInt(33) + 1;
            set.add(num);
        }
        int i = 0;
        for (int num : set) {
            lotteryNumber[i++] = num;
        }
        lotteryNumber[6] = random.nextInt(16) + 1;
        return lotteryNumber;
    }

    public static int[] getUserInput() {
        Scanner scanner = new Scanner(System.in);
        int[] userNumber = new int[7];
        Set<Integer> redSet = new HashSet<>();
        for (int i = 0; i < 6; i++) {
            while (true) {
                System.out.print("请输入红球号码，输入任意一个范围1-33的数字：");
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("号码超出范围，请重新输入！");
                } else if (redSet.contains(num)) {
                    System.out.println("该红球号码已存在，请重新输入！");
                } else {
                    userNumber[i] = num;
                    redSet.add(num);
                    break;
                }
            }
        }
        while (true) {
            System.out.print("请输入蓝球号码，输入任意一个范围1-16的数字：");
            int num = scanner.nextInt();
            if (num < 1 || num > 16) {
                System.out.println("号码超出范围，请重新输入！");
            } else {
                userNumber[6] = num;
                break;
            }
        }
        return userNumber;
    }

    public static void checkLottery(int[] lotteryNumber, int[] userNumber) {
        int redMatch = 0;
        int blueMatch = 0;
        List<Integer> redList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            redList.add(lotteryNumber[i]);
        }
        for (int i = 0; i < 6; i++) {
            if (redList.contains(userNumber[i])) {
                redMatch++;
            }
        }
        if (lotteryNumber[6] == userNumber[6]) {
            blueMatch++;
        }
        System.out.println("中奖号码为：");
        printArray(lotteryNumber);
        System.out.println("您投注号码为：");
        printArray(userNumber);
        System.out.println("您命中了" + redMatch + "个红色球号码，" + blueMatch + "个蓝色球号码。");
        if (redMatch == 6 && blueMatch == 1) {
            System.out.println("恭喜您，中了一等奖！");
        } else if (redMatch == 6 && blueMatch == 0) {
            System.out.println("恭喜您，中了二等奖！");
        } else if (redMatch == 5 && blueMatch == 1) {
            System.out.println("恭喜您，中了三等奖！");
        } else if ((redMatch == 5 && blueMatch == 0) || (redMatch == 4 && blueMatch == 1)) {
            System.out.println("恭喜您，中了四等奖！");
        } else if ((redMatch == 4 && blueMatch == 0) || (redMatch == 3 && blueMatch == 1)) {
            System.out.println("恭喜您，中了五等奖！");
        } else if ((redMatch == 2 && blueMatch == 1) || (redMatch == 1 && blueMatch == 1) || (redMatch == 0 && blueMatch == 1)) {
            System.out.println("恭喜您，中了六等奖！");
        } else {
            System.out.println("很遗憾，您未中奖！");
        }
    }

    public static void printArray(int[] array) {
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            if (i < array.length - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    public static void main(String[] args) {
        int[] lotteryNumber = generateLotteryNumber();
        int[] userNumber = getUserInput();
        checkLottery(lotteryNumber, userNumber);
    }
}
